import {
  psiMessagNumAPI
} from '@/api/psimanagement/psimessage'

/**
 * 消息记录
 */
const app = {
  state: {
    // 待办事项消息
    psimessageNum: {
      checkBorrowLeave: 0,
      checkInventoryLossLeave: 0,
      checkInventoryProfitEnter: 0,
      checkOtherEnter: 0,
      checkOtherLeave: 0,
      checkPurchaseEnter: 0,
      checkRepairEnter: 0,
      checkRepairLeave: 0,
      checkRetrunEnter: 0,
      checkSalesEnter: 0,
      checkSalesLeave: 0,
      checkTestEnter: 0,
      checkTestLeave: 0,
      totalNum: 0
    }
  },

  mutations: {
    /**
     * 更改待办事项
     */
    SET_PSIMESSAGENUM: (state, psimessageNum) => {
      let totalNum = 0
      for (const key in psimessageNum) {
        if (key != 'totalNum') {
          totalNum += (psimessageNum[key] || 0)
        }
      }
      psimessageNum.totalNum = totalNum
      state.psimessageNum = psimessageNum
    }
  },

  actions: {
    // 登录
    GetPsiMessageNum({
      state,
      commit
    }) {
      return new Promise((resolve, reject) => {
        psiMessagNumAPI()
          .then(response => {
            commit('SET_PSIMESSAGENUM', response.data)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    }

  }
}

export default app
